package com.swlz.servlet.admin;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import com.swlz.R;
import com.swlz.dto.MallSearch;
import com.swlz.dto.OrderSearch;
import com.swlz.entity.MallOrder;
import com.swlz.entity.MallOrderDetail;
import com.swlz.entity.MallProduct;
import com.swlz.service.MallOrderDetailService;
import com.swlz.service.MallOrderService;
import com.swlz.service.MallProductService;
import com.swlz.service.impl.MallOrderDetailServiceImpl;
import com.swlz.service.impl.MallOrderServiceImpl;
import com.swlz.service.impl.MallProductServiceImpl;
import com.swlz.servlet.HttpServletSupport;

@WebServlet("/admin/mallorder.let")
public class Order extends HttpServletSupport{

	private MallOrderService mallorderservice =new MallOrderServiceImpl();
	private MallOrderDetailService mallorderdetailservice =new MallOrderDetailServiceImpl();
	private MallProductService mallProductService=new MallProductServiceImpl();
public Object page(OrderSearch search) throws Exception {	
	return mallorderservice.pages(search);
	}

public Object orderStatue(){
	return mallorderservice.list();
	
}


//public Object deliver(MallOrder mallorder){
//	if(mallorder.getOrderStatus()==1||mallorder.getOrderStatus()==3) {
//		return R.error();
//	}else {
//		mallorder.setOrderStatus(2);
//	}
//	log.debug(mallorder.toString());
//	mallorderservice.update(mallorder);
//	return R.ok();
//	
//}

public String deliver(MallOrderDetail mallorderderDetail,HttpServletRequest request) throws Exception {
	
	
	if((mallorderservice.get((mallorderdetailservice.get(mallorderderDetail.getOrderDetailId())).getOrderId())).getOrderStatus()==0) {
		
		log.debug("买家还未付款");
		return "forward:/admin/product/order.jsp";
	}else if((mallorderservice.get((mallorderdetailservice.get(mallorderderDetail.getOrderDetailId())).getOrderId())).getOrderStatus()==2) {
		
		log.debug("商品已发货");
		return "forward:/admin/product/order.jsp";
	}
	else if((mallorderservice.get((mallorderdetailservice.get(mallorderderDetail.getOrderDetailId())).getOrderId())).getOrderStatus()==1) {
		
		MallOrder m=(mallorderservice.get((mallorderdetailservice.get(mallorderderDetail.getOrderDetailId())).getOrderId()));
		m.setOrderStatus(2);
		mallorderservice.update(m);
		
	}
	
	request.setAttribute("mallorder",mallorderservice.get((mallorderdetailservice.get(mallorderderDetail.getOrderDetailId())).getOrderId()));
	request.setAttribute("product",mallProductService.get((mallorderdetailservice.get(mallorderderDetail.getOrderDetailId())).getProductId()));
	log.debug(mallorderderDetail.toString());
	return "forward:/admin/product/success.jsp";
      }
  }